package cn.iocoder.yudao.module.call.controller.admin.listenactionallow;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.listenactionallow.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.listenactionallow.ListenActionAllowDO;
import cn.iocoder.yudao.module.call.service.listenactionallow.ListenActionAllowService;

@Tag(name = "管理后台 - 事件与动作")
@RestController
@RequestMapping("/call/listen-action-allow")
@Validated
public class ListenActionAllowController {

    @Resource
    private ListenActionAllowService listenActionAllowService;

    @PostMapping("/create")
    @Operation(summary = "创建事件与动作")
    @PreAuthorize("@ss.hasPermission('call:listen-action-allow:create')")
    public CommonResult<Integer> createListenActionAllow(@Valid @RequestBody ListenActionAllowSaveReqVO createReqVO) {
        return success(listenActionAllowService.createListenActionAllow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新事件与动作")
    @PreAuthorize("@ss.hasPermission('call:listen-action-allow:update')")
    public CommonResult<Boolean> updateListenActionAllow(@Valid @RequestBody ListenActionAllowSaveReqVO updateReqVO) {
        listenActionAllowService.updateListenActionAllow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除事件与动作")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:listen-action-allow:delete')")
    public CommonResult<Boolean> deleteListenActionAllow(@RequestParam("id") Integer id) {
        listenActionAllowService.deleteListenActionAllow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得事件与动作")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:listen-action-allow:query')")
    public CommonResult<ListenActionAllowRespVO> getListenActionAllow(@RequestParam("id") Integer id) {
        ListenActionAllowDO listenActionAllow = listenActionAllowService.getListenActionAllow(id);
        return success(BeanUtils.toBean(listenActionAllow, ListenActionAllowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得事件与动作分页")
    @PreAuthorize("@ss.hasPermission('call:listen-action-allow:query')")
    public CommonResult<PageResult<ListenActionAllowRespVO>> getListenActionAllowPage(@Valid ListenActionAllowPageReqVO pageReqVO) {
        PageResult<ListenActionAllowDO> pageResult = listenActionAllowService.getListenActionAllowPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ListenActionAllowRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出事件与动作 Excel")
    @PreAuthorize("@ss.hasPermission('call:listen-action-allow:export')")
    @OperateLog(type = EXPORT)
    public void exportListenActionAllowExcel(@Valid ListenActionAllowPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ListenActionAllowDO> list = listenActionAllowService.getListenActionAllowPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "事件与动作.xls", "数据", ListenActionAllowRespVO.class,
                        BeanUtils.toBean(list, ListenActionAllowRespVO.class));
    }

}